<?php

//ACTION DELETE INTRO
function del_intro() {

    global $db;

    $db->exec_delete('introduce',"`id`=".intval($_GET["del"]));

}

//ACTION DEL MULTI INTRO
function del_multi_intro() {

    global $db;

    if($_POST["del_multi"]) {

        $arr_id = $_POST["check"];
        foreach($arr_id as $i=>$id) {

            $db->exec_delete('introduce',"`id`=".intval($id));

        }

    }

}


//GET INTRO TO EDIT
function get_intro_edit() {

    global $db;

    if($_GET["id"]) {

        $edit_id = intval($_GET["id"]);

        $intro_edit = $db->query_first("SELECT * FROM introduce WHERE id =".$edit_id);

        if(!$edit_id) {

            redirect(ADMIN_URL);

        }

        return $intro_edit;
    }
}
//ACTION ADD NEW INTRO
function add_new_intro() {

    global $CONFIG,$db;

    if($_POST["add_new_intro"]) {

        $title 		= strip_tags($_POST["title"]);
        $content   	= $_POST["content"];

        $title_link = create_link($title);

        if(intro_title_is_exist($title_link)) {

            $title_link = $title_link."-".rand(1,999);

        }

        if(empty($title)) {

            $CONFIG["error_message"] = "<li>Tiêu đề không thể rỗng.</li>";

        }
        if(empty($content)) {

            $CONFIG["error_message"] .= "<li>Nội dung không thể rỗng.</li>";

        }


        if(empty($CONFIG["error_message"])) {

            $data = array();

            $data["title"] 		= $title;
            $data["title_link"] 	= $title_link;
            $data["content"] 		= $content;
            $data["lang"] 		= $_SESSION['adlang'];

            $db->exec_insert("introduce",$data);

            if(mysql_affected_rows()>0) {

                if($_POST["exit"]) {

                    server_redirect(ADMIN_URL."/index.php?module=intro");

                }

                $CONFIG["success_message"] .= "<li>Thêm mới bài Giới thiệu thành công.</li>";

                unset($_POST);

            }

        }


    }

}
//ACTION EDIT INTRO
function edit_intro() {

    global $CONFIG,$db;

    if($_POST["edit_intro"]) {

        $title 		= strip_tags($_POST["title"]);       
        $content   	= $_POST["content"];
        $edit_id		= intval($_POST["edit_id"]);

        if(empty($title)) {

            $CONFIG["error_message"] = "<li>Tiêu đề không thể rỗng.</li>";

        }
        if(empty($content)) {

            $CONFIG["error_message"] .= "<li>Nội dung không thể rỗng.</li>";

        }


        if(empty($CONFIG["error_message"])) {

            $data = array();

            $data["title"] 		= $title;          
            $data["content"] 		= $content;

            $db->exec_update("introduce",$data,'`id`='.$edit_id);

            if(mysql_affected_rows()>0) {

                if($_POST["exit"]) {

                    server_redirect(ADMIN_URL."/index.php?module=intro");

                }

                $CONFIG["success_message"] .= "<li>Lưu thay đổi thành công.</li>";

                unset($_POST);

            }

        }


    }

}
//GET ALL INTRO PAGE
function get_all_intro_page() {

    global $db,$CONFIG;


    $sel_all=$db->query_first("SELECT COUNT(id) m FROM introduce");

    $sql = "SELECT * FROM introduce WHERE lang='".$_SESSION['adlang']."' ORDER BY id";
    
    $page=intval($_GET["page"]) ? intval($_GET["page"]) :1;
    $page=intval($page);
    if ($page<0) $page=1;
    $number=$sel_all['m'];
    $limit = 10;
    $total=ceil($number/$limit);
    $start = (($page*$limit)-$limit);

    $intro = $db->query($sql." LIMIT ".$start.",".$limit);
    //Xu ly ban ghi cuoi cung thi sap xep lai trang tien den trang ke tiep
    if($page >1 &&$db->num_rows($intro)<1) {
        $page = $page -1;

        $total=ceil($number/$limit);
        $start = (($page*$limit)-$limit);

        $intro = $db->query($sql." LIMIT ".$start.",".$limit);

        server_redirect(get_string_url()."&page=".$page);
    }

    return array("intro"=>$intro,"page"=>$page,"total"=>$total);

}

function intro_title_is_exist($name) {
    global $db;

    $result = $db->query_first("SELECT title_link FROM introduce WHERE title_link = '".$name."'");

    if($result) {
        return true;
    } else {
        return false;
    }

}
?>